package com.springcloud.alibabanacosconfig.mq.service;

import com.springcloud.alibabanacosconfig.mq.dto.OrderTradeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/10/23 16:28
 * @description
 * @Modify By：
 * @ModifyTime： 2019/10/23
 * @Modify marker：
 **/
@Service
@Slf4j
public class OrderTradeServcie {
    @Autowired
    private Environment env;

    @Autowired
    public RabbitTemplate rabbitTemplate;


    public void createTrade(OrderTradeDTO orderTradeDTO){
        //TODO：创建交易记录

        orderTradeDTO.setStatus(1);
        final Long ttL = env.getProperty("order.trade.record.ttL",Long.class);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setExchange(env.getProperty("order.trade.delay.exchange.name"));
        rabbitTemplate.setRoutingKey("");
        rabbitTemplate.convertAndSend(orderTradeDTO,new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME,OrderTradeDTO.class.getName());
                message.getMessageProperties().setExpiration(ttL+"");
                return message;
            }
        });
    }
}
